# 将bug关联到个人，计算归一化后的可靠性
# 实际上不属于代码质量范畴，所以单独列出
import os
import sys

import pandas as pd
import math
from utils import run_cmd, get_bugs_github


class Reliability:
    def __init__(self, repo_path, commit_hash=''):
        proj_short_name = repo_path.split('/')[-1]
        df = pd.read_excel(f'bug2time/{proj_short_name}_github.xlsx')
        if not commit_hash:
            commit_hash = run_cmd('git rev-parse HEAD', repo_path)[0]
        time_str = run_cmd(f'git log --pretty=format:"%ci" {commit_hash} -1 | cat', repo_path)[0][:16]
        self.dts_file = df[df['time'] < time_str]
        self.reliability = self.get_reliability()
        # self.reliability.name = 'reliability'

    def get_reliability(self):
        # group_obj = self.dts_file.groupby('author')
        # counter_obj = group_obj.count()
        # 将dataframe变成series
        # counter_obj = counter_obj['number']
        # return counter_obj.apply(lambda n: math.pow(n, -1))
        return 1 / len(self.dts_file)


if __name__ == '__main__':
    # repo_path = 'projects/easyexcel'
    # remote_path = 'alibaba/easyexcel'
    os.chdir("D:/code_and_results")
    args = sys.argv
    repo_path = args[1]
    remote_path = args[2]
    get_bugs_github(remote_path)
    r = Reliability(repo_path)
    print(r.reliability)